/**
 * <strong>Title : UserWarehouseController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.mm.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2016年3月1日 下午1:44:12<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.MoveType;
import cn.b.sky.mes.mm.model.WarehouseType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.mes.mm.model.UserWarehouse;
import cn.b.sky.mes.mm.model.condition.MoveTypeCondition;
import cn.b.sky.mes.mm.model.condition.UserWarehouseCondition;
import cn.b.sky.mes.mm.service.MoveTypeService;
import cn.b.sky.mes.mm.service.UserWarehouseService;
import cn.b.sky.mes.mm.service.WarehouseTypeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.UserService;
import cn.b.sky.user.service.model.ECode;

/**
 * <strong>Title : UserWarehouseController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.controller<br></strong>
 * <strong>Description : </strong>仓库主数据<br> 
 * <strong>Create on : 2016年3月2日 下午1:44:12<br></strong>
 * <p> 
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人    |    修改日期    |    修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/userWarehouse/*")
public class UserWarehouseController extends SecurityController {

  @Resource
  UserWarehouseService userWarehouseService;

  @Resource
  UserService userService;

  @Autowired
  MoveTypeService moveTypeService;

  @Resource
  WarehouseTypeService warehouseTypeService;

  @Resource
  ECodeService eCodeService;

  @RequestMapping("userWarehouseList")
  public String selectDefectTypeListByCondition(String navTabId, Model model) {
    model.addAttribute("navTabId", navTabId);
    MoveTypeCondition con = new MoveTypeCondition();
    con.setSiteCode(getSiteCode());
    con.setLockFlag(0);
    con.setLanguageCode(SecurityContextUtil.getLanguageCode());
    List<MoveType> moveTypes = moveTypeService.selectAllMoveTypeList(con);
    model.addAttribute("moveTypes", moveTypes);
    return "mm/userWarehouse/userWarehouseList";
  }

  @RequestMapping("selectUserWarehouseList")
  @ResponseBody
  public DWZResult selectDefectTypeList(@ModelAttribute("condition") UserWarehouseCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<UserWarehouse> list = userWarehouseService.selectUserWarehouseListByCondition(condition);
    int count = userWarehouseService.selectUserWarehouseCountByCondition(condition);
    condition.setTotalCount(count);
    condition.setData(list);
    return ajaxDone(condition);
  }

  @RequestMapping("addUserWarehouse")
  public String addBom(Model model, String navTabId) {
    List<WarehouseType> types = warehouseTypeService.selectWarehouseTypeWithWarehouse(getSiteCode(), 0);
    model.addAttribute("warehouseTypes", types);
    setMoveTypeMap(model);
    model.addAttribute("navTabId", navTabId);
    return "mm/userWarehouse/userWarehouse_add";
  }

  private void setMoveTypeMap(Model model) {
    List<ECode> eCodes = eCodeService.selectECodeList(ECodeEnums.MOVEDIRECTIONTYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    HashMap<String, String> eCodesMap = new HashMap<String, String>();
    for (ECode ecode : eCodes) {
      eCodesMap.put(ecode.geteCode(), ecode.geteCodeText());
    }
    model.addAttribute("eCodesMap", eCodesMap);
    MoveTypeCondition condition = new MoveTypeCondition();
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    condition.setLanguageCode(SecurityContextUtil.getLanguageCode());
    HashMap<String, List<MoveType>> map = new HashMap<String, List<MoveType>>();
    List<MoveType> moveTypes = moveTypeService.selectAllMoveTypeList(condition);
    for (MoveType mt : moveTypes) {
      String ec = mt.getMoveDirectionECode();
      if (map.get(ec) == null) {
        List<MoveType> l = new ArrayList<MoveType>();
        l.add(mt);
        map.put(ec, l);
      } else {
        List<MoveType> l = map.get(ec);
        l.add(mt);
      }
    }
    model.addAttribute("moveTypeMap", map);
  }

  @RequestMapping("editUserWarehouse")
  public String editUserWarehouse(Model model, String navTabId, String userCode, String moveTypeCode,
      String warehouseCode) {
    String view = "mm/userWarehouse/userWarehouse_edit";
    if (StringUtils.isBlank(userCode) || StringUtils.isBlank(warehouseCode)) {
      return view;
    }
    List<UserWarehouse> userWarehouse = userWarehouseService.getUserWarehouse(userCode, warehouseCode, getSiteCode());
    model.addAttribute("userWarehouse", userWarehouse);
    setMoveTypeMap(model);
    model.addAttribute("navTabId", navTabId);
    return view;
  }

  @RequestMapping("viewUserWarehouse")
  public String viewUserWarehouse(Model model, String navTabId, String warehouseCode, String userCode) {
    String view = "mm/userWarehouse/userWarehouse_view";
    if (StringUtils.isBlank(userCode) || StringUtils.isBlank(warehouseCode)) {
      return view;
    }
    List<UserWarehouse> userWarehouse = userWarehouseService.getUserWarehouse(userCode, warehouseCode, getSiteCode());
    model.addAttribute("userWarehouse", userWarehouse);
    setMoveTypeMap(model);
    model.addAttribute("navTabId", navTabId);
    return view;
  }

  @RequestMapping("saveUserWarehouse")
  @ResponseBody
  public DWZResult saveUserWarehouse(Model model, String navTabId, String id, String warehouseCode, String userCode,
      String moveTypeCodes) {
    if (StringUtils.isBlank(warehouseCode) || StringUtils.isBlank(userCode) || StringUtils.isBlank(moveTypeCodes)) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    String[] mtCodes = moveTypeCodes.split(",");
    UserWarehouse userWarehouse = new UserWarehouse();
    setCreateInfo(userWarehouse);
    userWarehouse.setSiteCode(getSiteCode());
    userWarehouse.setUserCode(userCode);
    userWarehouse.setWarehouseCode(warehouseCode);
    if (StringUtils.isBlank(id)) {
      userWarehouseService.insertUserWarehouse(mtCodes, userWarehouse);
    } else {
      userWarehouseService.updateUserWarehouse(mtCodes, userWarehouse);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }

  @RequestMapping("deleteUserWarehouse")
  @ResponseBody
  public DWZResult deleteUserWarehouse(Model model, String navTabId, HttpServletRequest request) {

    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    String[] codes = ids.split(",");
    int len = codes.length;
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      String[] str = codes[i].split(";");
      if (str.length >= 2) {
        reports[i] = userWarehouseService.deleteUserWarehouse(str[0], str[1], getSiteCode());
        if (!reports[i].isSuccess()) {
          success = false;
        }
      }
    }
    if (success) {
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("checkWarehouseNotAssigned")
  @ResponseBody
  public boolean checkWarehouseNotAssigned(String userCode, String warehouseCode) {
    if (StringUtils.isBlank(userCode) || StringUtils.isBlank(warehouseCode)) {
      return true;
    }
    return userWarehouseService.getUserWarehouse(userCode, warehouseCode, getSiteCode()).size() == 0;
  }
}
